/**
 * MP Software.
 */
package models;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import play.data.validation.Required;
import play.db.jpa.Model;

/**
 * @author Jeremiah John M. Macariola
 *
 */
@Entity
public class PayrollPeriod extends Model {

    @Transient
    private final String DATE_FORMAT = "MMM dd, yyyy";

    @Transient
    private final String DATE_FORMAT_JQUERY = "MM/dd/yyyy";

    @Required
    public Date fromDate;

    @Required
    public Date toDate;

    @ManyToOne
    public Client client;

    public PayrollPeriod(Date fromDate, Date toDate, Client client) {
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.client = client;
    }

    public String getFromDateFormatted() {
        DateFormat df = new SimpleDateFormat(DATE_FORMAT);
        return df.format(fromDate);
    }

    public String getToDateFormatted() {
        DateFormat df = new SimpleDateFormat(DATE_FORMAT);
        return df.format(toDate);
    }

    public String getFromDateFormattedJQuery() {
        DateFormat df = new SimpleDateFormat(DATE_FORMAT_JQUERY);
        return df.format(fromDate);
    }

    public String getToDateFormattedJQuery() {
        DateFormat df = new SimpleDateFormat(DATE_FORMAT_JQUERY);
        return df.format(toDate);
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd");
        return sdf.format(fromDate) + " - " + sdf.format(toDate);
    }

}
